package com.fengwk.user.controller;

import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fengwk.support.util.ResultUtils;
import com.fengwk.user.face.annotation.Permission;
import com.fengwk.user.face.model.Role;
import com.fengwk.user.service.RoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Permission("role")
@Api(tags = "角色")
@RestController
@RequestMapping("/api/role")
public class RoleController {

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;
	
	@Autowired
	private RoleService roleService;
	
	@ApiOperation(
			value = "存储角色", 
			response = Role.class)
	@PostMapping("/saveRole")
	public void saveRole(
			@ApiParam(value = "角色", required = true) @RequestParam(value = "role", required = true) String role,
			@ApiParam(value = "描述", required = false) @RequestParam(value = "description", required = false) String description) {
		ResultUtils.returnJsonResult(request, response, roleService.saveRole(role, description));
	}
	
	@ApiOperation(
			value = "移除角色", 
			response = Role.class)
	@PostMapping("/removeRole")
	public void removeRole(
			@ApiParam(value = "角色id", required = true) @RequestParam(value = "roleId", required = true) Long roleId) {
		ResultUtils.returnJsonResult(request, response, roleService.removeRole(roleId));
	}
	
	@ApiOperation(
			value = "更新角色", 
			response = Role.class)
	@PostMapping("/updateRole")
	public void updateRole(
			@ApiParam(value = "角色id", required = true) @RequestParam(value = "roleId", required = true) Long roleId,
			@ApiParam(value = "角色", required = false) @RequestParam(value = "role", required = false) String role,
			@ApiParam(value = "描述", required = false) @RequestParam(value = "description", required = false) String description) {
		ResultUtils.returnJsonResult(request, response, roleService.updateRole(roleId, role, description));
	}
	
	@ApiOperation(
			value = "同步权限", 
			response = Void.class)
	@PostMapping("/synPermission")
	public void synPermission(
			@ApiParam(value = "目标角色id", required = true) @RequestParam(value = "roleId", required = true) Long roleId,
			@ApiParam(value = "同步权限id列表", required = true) @RequestParam(value = "permissionIds", required = true) Collection<Long> permissionIds) {
		roleService.synPermission(roleId, permissionIds);
		ResultUtils.returnJsonResult(request, response, true);
	}
	
	@ApiOperation(
			value = "添加角色权限", 
			response = Void.class)
	@PostMapping("/addPermission")
	public void addPermission(
			@ApiParam(value = "添加目标角色id", required = true) @RequestParam(value = "roleId", required = true) Long roleId,
			@ApiParam(value = "添加的权限id列表", required = true) @RequestParam(value = "permissionIds", required = true) Collection<Long> permissionIds) {
		roleService.addPermission(roleId, permissionIds);
		ResultUtils.returnJsonResult(request, response, true);
	}
	
	@ApiOperation(
			value = "移除角色权限", 
			response = Void.class)
	@PostMapping("/removePermission")
	public void removePermission(
			@ApiParam(value = "移除目标角色id", required = true) @RequestParam(value = "roleId", required = true) Long roleId,
			@ApiParam(value = "移除的权限id列表", required = true) @RequestParam(value = "permissionIds", required = true) Collection<Long> permissionIds) {
		roleService.removePermission(roleId, permissionIds);
		ResultUtils.returnJsonResult(request, response, true);
	}
	
	@ApiOperation(
			value = "通过用户id获取角色列表", 
			response = Role.class)
	@PostMapping("/listRoleByUserId")
	public void listRoleByUserId(
			@ApiParam(value = "用户id", required = true) @RequestParam(value = "userId", required = true) Long userId) {
		ResultUtils.returnJsonResult(request, response, roleService.listRoleByUserId(userId));
	}
	
	@ApiOperation(
			value = "分页获取角色", 
			response = Role.class)
	@PostMapping("/pageRole")
	public void pageRole(
			@ApiParam(value = "页码", required = true) @RequestParam(value = "pageNum", required = true) Integer pageNum,
			@ApiParam(value = "每页大小", required = true) @RequestParam(value = "pageSize", required = true) Integer pageSize) {
		ResultUtils.returnJsonResult(request, response, roleService.pageRole(pageNum, pageSize));
	}
	
}
